const app = getApp();
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year+'年', month+'月', day+'日'].map(formatNumber).join('')+ ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const formatDate = date =>{
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  month = month < 10 ? ('0' + month) : month;
  let day = date.getDate();
  day = day < 10 ? ('0' + day) : day;
  let hour = date.getHours();
  hour = hour < 10 ? ('0' + hour) : hour;
  let minute = date.getMinutes();
  minute = minute < 10 ? ('0' + minute) : minute;
  let data = year + "-" + month + "-" + day
  let time = hour + ":" + minute

  return [data,time]
}
const extend = (data,dataExtend) => {
  var res={};
  for (var key in data) {
    res[key] = data[key];
  } 
  for (var key in dataExtend) {
    res[key] = dataExtend[key]; 
  } 
  return res;
}

function isLoged() {
  if(app.globalData.logged){
    return true;
  } else{
    return false;
  }
}

function chenageLogin(){
  app.globalData.logged = true;
}

module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  extend:extend,
  isLoged:isLoged,
  chenageLogin:chenageLogin
}
